<?php
require_once '../forward.php';

if (!empty($_POST)) {
    if (isset($_POST['username'])) $username = trim($_POST['username']);
        else $username = '';
    if (isset($_POST['password'])) $password = $_POST['password'];
        else $password = '';
    if (isset($_POST['password2'])) $password2 = $_POST['password2'];
        else $password2 = '';
    if (isset($_POST['firstname'])) $firstname = trim($_POST['firstname']);
        else $firstname = '';
    if (isset($_POST['lastname'])) $lastname = trim($_POST['lastname']);
        else $lastname = '';
    $check = check_username($username);
    if ($check !== true) $e1 = $check;
    $check = check_password($password);
    if ($check !== true) $e2 = $check;
    if ($password2 != $password && !isset($e2)) $e3 = 'repeated password is not the same';
    $check = check_firstname($firstname);
    if ($check !== true) $e4 = $check;
    $check = check_lastname($lastname);
    if ($check !== true) $e5 = $check;
    if (!isset($e1) && !isset($e2) && !isset($e3) && !isset($e4) && !isset($e5)) {
        $user = create_new_user($username, $password, $firstname, $lastname);
    }
}

render_begin(' :: sign up');
?>
<div id="logintip">
<a href="..">log in</a>
</div>
<?php
render_page_begin();
?>
<div class="centered">
<?php if (!isset($user)) { ?>
<div id="signupinfo">
<form method="POST">
username: <input name="username" type="text" maxlength="32" tabindex="1"<?php if (isset($username)) echo " value=\"$username\"";?>/><br/>
<?php if (isset($e1)) { ?><div class="wrong"><?php echo $e1; ?></div><?php } ?>
password: <input name="password" type="password" tabindex="2"/><br/>
<?php if (isset($e2)) { ?><div class="wrong"><?php echo $e2; ?></div><?php } ?>
repeat password: <input name="password2" type="password" tabindex="3"/><br/>
<?php if (isset($e3)) { ?><div class="wrong"><?php echo $e3; ?></div><?php } ?>
<br/>
first name: <input name="firstname" type="text" maxlength="32" tabindex="4"<?php if (isset($firstname)) echo " value=\"$firstname\"";?>/><br/>
<?php if (isset($e4)) { ?><div class="wrong"><?php echo $e4; ?></div><?php } ?>
last name: <input name="lastname" type="text" maxlength="32" tabindex="5"<?php if (isset($lastname)) echo " value=\"$lastname\"";?>/><br/>
<?php if (isset($e5)) { ?><div class="wrong"><?php echo $e5; ?></div><?php } ?>
<br/>
<input id="signupbtn" type="submit" tabindex="5" value="Sign up"/>
</form>
</div>
<?php } else { ?>
<div id="signupnotice">
Hi, <strong><?php echo $user['first_name']; ?></strong>! Your username is <span class="username"><?php echo $user['username']; ?></span><br/>
Use <a href="..">this link</a> to log in the system.
</div>
<?php } ?>
</div>
<?php
render_page_end();
render_end();
?>
